package com.tsky.system.manager.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.tsky.casing.QueryBean;
import com.tsky.casing.orm.impl.EntityManagerImpl;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Menu;
import com.tsky.system.manager.MenuManager;
import com.tsky.system.query.MenuQuery;

@Repository("menuManager")
public class MenuManagerImpl extends EntityManagerImpl implements MenuManager {

	public void query(QueryBean queryBean) {
		// TODO Auto-generated method stub
		MenuQuery query = (MenuQuery) queryBean;
		
		// 基本查询条件
		int page = queryBean.getPage();
		int pageSize = queryBean.getRows(); // page size
		String orderField = queryBean.getSort();
		String sortOrder = queryBean.getOrder();
		
		// 组合查询条件
		String name = query.getName();
		String status = query.getStatus();
		String url = query.getUrl();
		
		Criteria ctr = this.getSession().createCriteria(Menu.class);

		// 组合查询
		if (!BeanUtil.isEmpty(status) && !"3".equals(status)) {
			ctr.add(Restrictions.eq("status", status));
		}
		
		if (!BeanUtil.isEmpty(name)) {
			ctr.add(Restrictions.like("name", "%" + name + "%"));
		}
		
		if (!BeanUtil.isEmpty(url)) {
			ctr.add(Restrictions.like("url", "%" + url + "%"));
		}

		// 排序
		if (!BeanUtil.isEmpty(sortOrder)) {
			if ("asc".equals(sortOrder)) {
				ctr.addOrder(Order.asc(orderField));
			} else {
				ctr.addOrder(Order.desc(orderField));
			}
		}
		
		// 总记录数
		List<?> resultList = ctr.list();
		queryBean.setTotal(resultList.size());

		ctr.setFirstResult(pageSize * (page - 1));
		ctr.setMaxResults(pageSize);
		
		queryBean.setDataList(ctr.list());
	}

	public List getRoleMenus1(String userid) {
		String sql = "select m.id, m.name, m.url, m.layerNO, m.pid, m.property " +
				"from t_SYS_user u, t_SYS_role r, t_SYS_menu m, t_SYS_rolemenu rm "+
				"where u.roleid=r.id "+
				"and r.id=rm.roleid "+
				"and m.id=rm.menuid "+
				"and u.id=? "+
				"order by layerNO";
		
		List resultList = this.getSession().createSQLQuery(sql).setString(0, userid).list();
		return resultList;
	}
	
	public List getRoleMenus(String userid) {
		String sql = "select m.id, m.name, m.url, m.property, m.pid, m.orderNO, m.icon " +
				"from t_SYS_user u, t_SYS_role r, t_SYS_menu m, t_SYS_rolemenu rm "+
				"where u.roleid=r.id "+
				"and r.id=rm.roleid "+
				"and m.id=rm.menuid "+
				"and u.id=? "+
				"order by pid, property desc, orderNO asc";
		
		List resultList = this.getSession().createSQLQuery(sql).setString(0, userid).list();
		return resultList;
	}

	public List getMenus() {
		// TODO Auto-generated method stub
		String sql = "select m.id, m.name, m.url, m.property, m.pid, m.orderNO, m.icon " +
		"from t_SYS_menu m "+
		"where m.status=1 "+
		"order by pid, property desc, orderNO asc";

		List resultList = this.getSession().createSQLQuery(sql).list();
		return resultList;
	}

	public List<?> getConfigMenus(String roleid) {
		// TODO Auto-generated method stub
		String sql = "select cm.id, cm.name, cm.url, cm.property, cm.pid,"+
			"(case when cm.id in( "+
			"select m.id "+
			"from t_SYS_role r, t_SYS_menu m, t_SYS_rolemenu rm "+
			"where r.id=rm.roleid "+
			"and m.id=rm.menuid "+
			"and r.id=? "+
			") then 'true' else 'false' end) checked, cm.icon "+
			"from t_SYS_menu cm "+
			"order by cm.pid, cm.property desc, cm.orderNO asc";

		List resultList = this.getSession().createSQLQuery(sql).setString(0, roleid).list();
		return resultList;
	}

	public List<?> getPMenus() {
		// TODO Auto-generated method stub
		String sql = "select m.id, m.name, m.url, m.property, m.pid, m.orderNO, m.icon " +
		"from t_SYS_menu m "+
		"where m.status=1 "+
		"and m.property=1 "+
		"order by pid, property desc, orderNO asc";

		List resultList = this.getSession().createSQLQuery(sql).list();
		return resultList;
	}
	
}
